/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PaqueteForm.java
 *
 * Created on 07/04/2010, 12:16:30 PM
 */

package agencia.form;

import agencia.Agencia;
import agencia.LugarTuristico;
import agencia.TourPaquete;
import agencia.controller.AgenciaController;
import agencia.controller.LugarTuristicoController;
import agencia.controller.PaqueteController;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author elvis
 */
public class PaqueteForm extends javax.swing.JInternalFrame {

    private AgenciaController agenciaController;
    private LugarTuristicoController lugarTuristicoController;
    private List<LugarTuristico> lugares;
    private PaqueteController paqueteController;
    private PaqueteListaForm paqueteListaForm;
    private TourPaquete paquete;
    private int accion = 1;
    public static int ACCION_NUEVO = 1 ;
    public static int ACCION_MODIFICAR = 2 ;

    public void setAgenciaController(AgenciaController agenciaController) {
        this.agenciaController = agenciaController;
    }
    public void setPaqueteController(PaqueteController paqueteController) {
        this.paqueteController = paqueteController;
    }
    public void setPaqueteListaForm(PaqueteListaForm paqueteListaForm) {
        this.paqueteListaForm = paqueteListaForm;
    }
    public void setPaquete(TourPaquete paquete) {
        this.paquete = paquete;
    }
    public void setAccion(int accion) {
        this.accion = accion;
    }
    public void setLugarTuristicoController(LugarTuristicoController lugarTuristicoController) {
        this.lugarTuristicoController = lugarTuristicoController;
    }

    /** Creates new form PaqueteForm */
    public PaqueteForm() {
        lugares = new ArrayList<LugarTuristico>();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGrabar = new javax.swing.JButton();
        btncancelar = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        txtNombre = new javax.swing.JTextField();
        txttarifaadulto = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txttarifaninio = new javax.swing.JTextField();
        cbxAgencia = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        txttarifamayor = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstDisponibles = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstSeleccionados = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        btnAll1 = new javax.swing.JButton();
        btnAll2 = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        setClosable(true);
        setTitle("Datos de Paquete");

        btnGrabar.setText("Grabar");
        btnGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGrabarActionPerformed(evt);
            }
        });

        btncancelar.setText("Cancelar");

        jLabel6.setText("Agencia");

        cbxAgencia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel5.setText("Tarifa Adulto Mayor");

        jLabel3.setText("Tarifa Niños");

        jLabel4.setText("Tarifa Adulto");

        jLabel1.setText("Codigo");

        jLabel2.setText("Nombre");

        txtCodigo.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(43, 43, 43)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .addComponent(cbxAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txttarifamayor))
                                .addComponent(txttarifaadulto, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txttarifaninio, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))))
                    .addComponent(jLabel5))
                .addGap(67, 67, 67))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cbxAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txttarifaninio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txttarifaadulto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txttarifamayor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Datos del Paquete", jPanel1);

        lstDisponibles.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(lstDisponibles);

        lstSeleccionados.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(lstSeleccionados);

        jButton1.setText(">");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnAll1.setText(">>");
        btnAll1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAll1ActionPerformed(evt);
            }
        });

        btnAll2.setText("<<");
        btnAll2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAll2ActionPerformed(evt);
            }
        });

        btnBack.setText("<");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnAll2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAll1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnBack, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                .addGap(20, 20, 20)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(43, 43, 43)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAll1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAll2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBack))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap(34, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(27, 27, 27))
        );

        jTabbedPane1.addTab("Lugares Incluidos", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(btnGrabar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                .addComponent(btncancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(81, 81, 81))
            .addComponent(jTabbedPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btncancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGrabar, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGrabarActionPerformed
        // TODO add your handling code here:
        paquete.setAgencia(agenciaController.obtenerAgencia(cbxAgencia.getSelectedItem().toString()));
        paquete.setNombre(txtNombre.getText());
        paquete.setTarifaNinio(Double.parseDouble(txttarifaninio.getText()));
        paquete.setTarifaAdulto(Double.parseDouble(txttarifaadulto.getText()));
        paquete.setTarifaadultoMayor(Double.parseDouble(txttarifamayor.getText()));
        
        if(accion==ACCION_NUEVO){
            paqueteController.registrarNuevoPaquete(paquete);
        }
        JOptionPane.showMessageDialog(rootPane, "La informacion de paquete ha sido grabada");
        paqueteListaForm.pintagrilla(paqueteController.getListaToursPaquetes(paquete.getAgencia().getCodigo()));
        this.dispose();
    }//GEN-LAST:event_btnGrabarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO saca el elemento seleccionado
        //y lo agrega en la otra lista
        String sel = lstDisponibles.getSelectedValue().toString();
        LugarTuristico lsel = null;
        for(  LugarTuristico lugar : lugares){
            if(lugar.getNombre().equals(sel)){
               lsel = lugar;
            }
        }
        paquete.getLugares().add(lsel);
        lugares.remove(lsel);
        recargarListas();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnAll1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAll1ActionPerformed
        // TODO pas todos los disponibles a la lista de seleccionados
        for(LugarTuristico l : lugares){
            paquete.getLugares().add(l);
        }
        lugares = new ArrayList<LugarTuristico>();
        recargarListas();
    }//GEN-LAST:event_btnAll1ActionPerformed

    private void btnAll2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAll2ActionPerformed
        // TODO add your handling code here:
         for(LugarTuristico l : paquete.getLugares()){
           lugares.add(l);
        }
        paquete.setLugares(new ArrayList<LugarTuristico>());
        recargarListas();
    }//GEN-LAST:event_btnAll2ActionPerformed

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
        // TODO regreso un seleccioado a la lista disponibles
        String sel = lstSeleccionados.getSelectedValue().toString();
        LugarTuristico lsel = null;
        for(  LugarTuristico lugar : paquete.getLugares()){
            if(lugar.getNombre().equals(sel)){
               lsel = lugar;
            }
        }
        lugares.add(lsel);
        paquete.getLugares().remove(lsel);
        recargarListas();
    }//GEN-LAST:event_btnBackActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAll1;
    private javax.swing.JButton btnAll2;
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnGrabar;
    private javax.swing.JButton btncancelar;
    private javax.swing.JComboBox cbxAgencia;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JList lstDisponibles;
    private javax.swing.JList lstSeleccionados;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txttarifaadulto;
    private javax.swing.JTextField txttarifamayor;
    private javax.swing.JTextField txttarifaninio;
    // End of variables declaration//GEN-END:variables

   
    public void iniciaForm(){
        cbxAgencia.removeAllItems();
        for(Agencia agencia : agenciaController.getListaAgencias()){
            cbxAgencia.addItem(agencia.getNombre());
        }
        
        lugares.addAll( lugarTuristicoController.getLugaresTuristicos());

        System.out.println(lugares.size());
        if(accion==ACCION_NUEVO){
            paquete = new TourPaquete();
            
        }else{
            txtCodigo.setText(paquete.getCodigo()+"");
            txtNombre.setText(paquete.getNombre());
            txttarifaninio.setText(paquete.getTarifaNinio()+"");
            txttarifaadulto.setText(paquete.getTarifaAdulto()+"");
            txttarifamayor.setText(paquete.getTarifaadultoMayor()+"");
            cbxAgencia.setSelectedItem(paquete.getAgencia().getNombre());

            List<LugarTuristico>  lstEl = new ArrayList<LugarTuristico>();
            for(  LugarTuristico lugar : paquete.getLugares()){
                for(  LugarTuristico lugar2 : lugares){
                    if(lugar.getNombre().equals(lugar2.getNombre())){
                        lstEl.add(lugar2);
                    }
                }
            }

            for(LugarTuristico lugar2 : lstEl){
                lugares.remove(lugar2);
            }

        }

        recargarListas();

    }

    public void recargarListas(){

        DefaultListModel tabla = new DefaultListModel();
        for ( LugarTuristico lugar : paquete.getLugares() ) {
            tabla.addElement(lugar.getNombre());
        }
        lstSeleccionados.setModel(tabla);

        tabla = new DefaultListModel();
        for ( LugarTuristico lugar : lugares  ) {
            tabla.addElement(lugar.getNombre());
        }
        lstDisponibles.setModel(tabla);
    }

    

}
